package net.wantongtech.xa.uploadbasicinfo.service;

import com.wtkj.vo.KeyValue;
import com.wtkj.vo.dzzf.base.kfwd.CardInfo;
import com.wtkj.vo.dzzf.base.qxsjz.entity.IntegratedTransOut;
import com.wtkj.zwxt.enums.SignSourceEnum;
import net.wantongtech.xa.uploadbasicinfo.config.TollBasicInfoConfig;
import net.wantongtech.xa.uploadbasicinfo.dao.AlterChannelTypeDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Optional;

import static com.wtkj.constant.NewClearingConstant.*;
import static com.wtkj.vo.dzzf.share.staticvalue.CardStaticValue.EcardType.CARD_TYPE_JZ;

/**
 * 修复错误channelType数据的问题
 *
 * @author MaShibo
 * 日期 :  2020/1/12
 */
@Service
public class AlterErrorChannelTypeDataService {


    @Resource
    private TollBasicInfoConfig tollBasicInfoConfig;


    @Resource
    private AlterChannelTypeDao alterChannelTypeDao;


    @Transactional(rollbackFor = Throwable.class)
    public void alter() {
        Map<String, IntegratedTransOut> integratedTransOutMap = alterChannelTypeDao.queryIntegratedTransOutMap();
        integratedTransOutMap.forEach((key, integratedTransOut) -> {
            String etcCardId = integratedTransOut.getCardId();
            String obuId = integratedTransOut.getObuId();
            KeyValue<String, String> kv = determineChannelTypeAndSignSource(etcCardId, obuId);
            String rightChannelType = kv.getId();
            String rightSignSource = kv.getValue();
            if (!StringUtils.equals(rightChannelType, integratedTransOut.getChannelType())) {
                IntegratedTransOut update = new IntegratedTransOut();
                update.setChannelType(rightChannelType);
                update.setSignSource(rightSignSource);
                update.setIntegratedId(integratedTransOut.getIntegratedId());
                alterChannelTypeDao.updateObject(update);
            }

        });
    }


    private KeyValue<String, String> determineChannelTypeAndSignSource(String etcCardId, String obuId) {
        //若卡号不为空则查询并填充卡片信息
        boolean hasCardId = StringUtils.isNotBlank(etcCardId);

        if (!hasCardId && StringUtils.isNotBlank(obuId)) {
            Optional<String> optional = alterChannelTypeDao.queryCardNoByObuId(obuId);
            etcCardId = optional.orElse(null);
            hasCardId = StringUtils.isNotBlank(etcCardId);
        }

        String channelType = null;
        String signSource = null;
        if (hasCardId) {

            String cardNetId = etcCardId.substring(0, 4);
            String eCardNo = etcCardId.substring(4);
            CardInfo cardInfo = alterChannelTypeDao.queryCardInfoByNo(eCardNo);

            ///6101记账卡从cardInfo中获取对应的渠道信息
            if (JT_CARD_NET_ID.equals(cardNetId)) {
                if (cardInfo != null && cardInfo.getECardType() == CARD_TYPE_JZ) {
                    channelType = cardInfo.getChannelType();
                    signSource = cardInfo.getSignSource();
                }
                // 6107确定渠道信息
            } else if (SIX_BANK_NET_ID.equals(cardNetId)) {
                String sectionNo = etcCardId.substring(11, 13);
                channelType = tollBasicInfoConfig.getChannelTypeBySectionNo(sectionNo);
                //无法确定渠道信息的都为"光大"
                if (channelType == null) {
                    channelType = CEB_CHANNEL_TYPE;
                }
                signSource = SignSourceEnum.SELF_PUBLISH.getSource();
            } else {
                channelType = tollBasicInfoConfig.getChannelTypeByCardNetId(cardNetId);
                signSource = SignSourceEnum.SELF_PUBLISH.getSource();
            }
        }

        return new KeyValue<>(channelType, signSource);
    }

}
